package com.fzj.example.socket;

import java.io.IOException;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.UnknownHostException;
import java.util.Enumeration;

/**
 * ClassName: InetAddressDemo
 * date: 2019/11/14 19:34
 * Description: TODO
 *
 * @author fanzj@bonree.com
 * @since JDK 1.8
 */
public class InetAddressDemo {
    public static void main(String[] args) {
        try {
            InetAddress inetAddress=InetAddress.getByName("www.fzjweb.top");
            InetAddress inetAddress1=InetAddress.getByName("127.0.0.1");
            InetAddress inetAddress2=InetAddress.getByName("192.168.0.23");
            InetAddress inetAddress3=InetAddress.getByName("localhost");
            System.out.println("getAddress:"+inetAddress.getAddress());
            System.out.println("getHostName:主机别名:"+inetAddress.getHostName());
            System.out.println("getHostAddress:"+inetAddress.getHostAddress());
            System.out.println("getCanonicalHostName:主机名:"+inetAddress.getCanonicalHostName());
            System.out.println("isReachable:"+inetAddress.isReachable(80));
            System.out.println("getAddress:"+inetAddress2.getAddress());
            System.out.println("getHostName:"+inetAddress2.getHostName());
            System.out.println("getHostAddress:"+inetAddress2.getHostAddress());
            System.out.println("getCanonicalHostName:"+inetAddress2.getCanonicalHostName());
            System.out.println(inetAddress.getHostAddress()+" isLinkLocalAddress:"+inetAddress.isLinkLocalAddress());
            System.out.println(inetAddress.getHostAddress()+" isLoopbackAddress:"+inetAddress.isLoopbackAddress());
            System.out.println(inetAddress1.getHostAddress()+" isLinkLocalAddress:"+inetAddress1.isLinkLocalAddress());
            System.out.println(inetAddress1.getHostAddress()+" isLoopbackAddress:"+inetAddress1.isLoopbackAddress());
            System.out.println(inetAddress2.getHostAddress()+" isLinkLocalAddress:"+inetAddress2.isLinkLocalAddress());
            System.out.println(inetAddress2.getHostAddress()+" isLoopbackAddress:"+inetAddress2.isLoopbackAddress());
            System.out.println(inetAddress3.getHostAddress()+" isLinkLocalAddress:"+inetAddress3.isLinkLocalAddress());
            System.out.println(inetAddress3.getHostAddress()+" isLoopbackAddress:"+inetAddress3.isLoopbackAddress());
            //设定服务器地址
            InetAddress inetAddress4=InetAddress.getLocalHost();
            InetAddress inetAddress5=InetAddress.getByName("www.baidu.com");
            InetAddress inetAddress6=InetAddress.getByName("192.168.0.23");
            //单一网卡获取
            String ip=InetAddress.getLocalHost().getHostAddress();
            System.out.println("单一网卡获取ip:"+ip);
            //多网卡获取
            System.out.println("多网卡获取ip");
            Enumeration< NetworkInterface > networkInterfaces = NetworkInterface.getNetworkInterfaces();
            while (networkInterfaces.hasMoreElements()){
                NetworkInterface networkInterface = networkInterfaces.nextElement();
                Enumeration< InetAddress > inetAddresses = networkInterface.getInetAddresses();
                while (inetAddresses.hasMoreElements()){
                    InetAddress inetAddress7 = inetAddresses.nextElement();
                    System.out.println("ip:"+inetAddress7.getHostAddress());
                    System.out.println("***********");
                }
            }

        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static boolean isReachIp(InetAddress inetAddress,String ip){
        boolean isReach=false;
        if(inetAddress instanceof  java.net.Inet4Address){
            System.out.println(ip+"is ipv4");
        }
        if(inetAddress instanceof  java.net.Inet6Address){
            System.out.println(ip+"is ipv6");
        }else{
            System.out.println(ip+"is unrecongized");
        }
        //时间范围是否可达
        try {
            if(inetAddress.isReachable(10000)){
                isReach=true;
                return isReach;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return isReach;
    }
}
